/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#pragma once

#include "../../tools/Tools.h"

class World;
class Channel;
class Selector;

class Worlds {
private:
	static const char* names [];
	vector <World*> worlds;
	static Worlds* instance;
	char channelcount;
	char worldcount;
	string defaultHeader;
	string eventmsg;
	Selector* selector;
	IP ip;
	int questExpRate;
	int expRate;
	int dropRate;
	int mesoRate;
	int userLimit;
	static short port;
public:
	Worlds() {
		instance = this;
	}
	Selector *getSelector() {
		return selector;
	}
	void start(Selector* selector) {
		this->selector = selector;
		for(int i=0; i<worldcount; i++)
			newWorld(i, channelcount);
	}
	void newWorld(int id, char channels);
	static Worlds* getInstance() {
		if(instance == NULL)
			return new Worlds();
		return instance;
	}
	void setChannelsCount(char c) {
		channelcount = c;
	}
	void setWorldsCount(char c) {
		worldcount = c;
	}
	void setIP(IP ip) {
		this->ip = ip;
	}
	IP* getIP() {
		return &ip;
	}
	char getWorldsCount() {
		return worldcount;
	}
	void setQuestExpRate(int qexp) {
		questExpRate = qexp;
	}
	int getQuestExpRate() {
		return questExpRate;
	}
	void setExpRate(int exp) {
		this->expRate = exp;
	}
	int getExpRate() {
		return expRate;
	}
	void setDropRate(int drop) {
		this->dropRate = drop;
	}
	int getDropRate() {
		return dropRate;
	}
	void setMesoRate(int meso) {
		this->mesoRate = meso;
	}
	int getMesoRate() {
		return mesoRate;
	}
	World* getWorld(unsigned int id) {
		if(id > worlds.size())
			return NULL;
		return worlds[id];
	}
	void setPort(short port) {
		this->port = port;
	}
	static short getPort() {
			return port++;
	}
	Channel* getChannelByPort(short port);
	static string getName(int id) {
		if(id >= 0 && id < 21)
			return string(names[id]);
		return string("");
	}
	void setDefaultHeader(string a) {
		defaultHeader = a;
	}
	string getDefaultHeader() {
		return defaultHeader;
	}
	void setEventMessage(string msg) {
		eventmsg = msg;
	}
	string getEventMessage() {
		return eventmsg;
	}
	void setUserLimit(int limit) {
		userLimit = limit;
	}
	int getUserLimit() {
		return userLimit;
	}
	void commandListener();
	bool isPlayerConnected(int charid);
	string handleCommand(string cmd);
};
